//
//  File.m
//  Cell
//
//  Created by Orbitus on 6/13/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "File.h"


@implementation File

- (id) initWithPath:(id) inpath
{
	self = [super init];
	
	if (self != nil)
	{
		path = [[inpath stringByExpandingTildeInPath] retain];
		name = [[path lastPathComponent] retain]; //hang on to the path
		
		image = [[NSImage alloc] initWithContentsOfFile:[[[[NSBundle mainBundle] bundlePath] 
														  stringByDeletingLastPathComponent]
														 stringByAppendingString:@"/Sequences/FolderPic.gif"]];				
	
		if ([[self name] hasSuffix:@".acedb"])
		{
			image = [[NSImage alloc] initWithContentsOfFile:[[[[NSBundle mainBundle] bundlePath] 
															  stringByDeletingLastPathComponent]
															 stringByAppendingString:@"/Sequences/acedb.gif"]];			
		}
		if ([[self name] hasSuffix:@".pdb"])
		{
			image = [[NSImage alloc] initWithContentsOfFile:[[[[NSBundle mainBundle] bundlePath] 
															  stringByDeletingLastPathComponent]
															 stringByAppendingString:@"/Sequences/pdb.gif"]];			
		}
		if ([[self name] hasSuffix:@".embl"])
		{
			image = [[NSImage alloc] initWithContentsOfFile:[[[[NSBundle mainBundle] bundlePath] 
															  stringByDeletingLastPathComponent]
															 stringByAppendingString:@"/Sequences/embl.gif"]];			
		}
		if ([[self name] hasSuffix:@".fasta"])
		{
			image = [[NSImage alloc] initWithContentsOfFile:[[[[NSBundle mainBundle] bundlePath] 
															  stringByDeletingLastPathComponent]
															 stringByAppendingString:@"/Sequences/fasta.gif"]];			
		}

	}
	return self;
}

- ( NSString *) name
{
	return name;
}
- ( NSString *) path
{
	return path;
}

- (BOOL) isLeaf
{
	BOOL isDirectory = NO;
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];
    
	if (isDirectory && exists)
	{
		return  NO;
	}
	
	return YES;
	
}
- (id) files{
	if (files == nil){
		files = [NSMutableArray new];
		
		
		id names = [[NSFileManager defaultManager ] directoryContentsAtPath:path];
		

		
		for (NSString *fileName in names)
		{
			if (! [fileName hasPrefix:@"."]) {
				File *file = [[File alloc] initWithPath:[path stringByAppendingPathComponent:fileName]];
				[files addObject:file];
				[file release];
			}
		}
		
		
	}
	return files;
}

@end
